<?php
namespace app\mapi\controller;
use app\mapi\model\UserListModel;
use app\mapi\model\MallConfsModel;
use think\Controller;
use com\Aes;

header("Access-Control-Allow-Origin: *");

/**
 * 继承控制器
 * @author  黄明杰 vx:HMJ125855
 * @version 20190201
 */
class Base extends Controller{

	# 访问空的方法
    public function _empty(){
        return $this->fetch('PublicBase/show404');
    }

	# 初始信息方法
	protected function _initialize(){
        # 商城配置
        $MallConfig = $this->MallConfigCache();
        
        config($MallConfig);
    }

    /**
     * 获取登录信息
     * @return  Int
     */
	protected function checkLogin($Token = Null){
        if(!empty($Token)){
            $Aes = new Aes();

            # Aes解密
            $Token = $Aes->decrypt($Token);

            $UserList = new UserListModel;

            $FindUser = $UserList->mInfo(['user_id'=>$Token, 'user_state'=>1], 'user_id,user_account,user_mobile,user_create');

            if(!empty($FindUser)){
                return $FindUser;
            }else{
                $this->ShowAjax(3, '请重新登陆');
            }
        }else{
            $this->ShowAjax(3, '请重新登陆');
        }
	}

    /**
     * 获取商城配置缓存信息
     * @return  Array
     */
    protected function MallConfigCache(){
        $MallConfig = cache('MallConfig');

        if(empty($MallConfig)){
            $Array = $this->MallConfigLists();
        }else{
            $Array = $MallConfig;
        }

        return $Array;
    }

    /**
     * 获取商城配置列表信息
     * @return  Array
     */
    protected function MallConfigLists(){
        $MallConfig = new MallConfsModel;

        $Lists = $MallConfig->mAll([], '*');

        $Array = [];

        if(!empty($Lists)){
            foreach($Lists as $k => $v){
                $Array[$v['confs_name']] = $v['confs_value'];
            }

            cache('MallConfig', $Array);
        }

        return $Array;
    }

}